﻿<?php
class UserStats 
{
	public static function usersOnline($datafile, $seconds)
	{
		$ip = getenv("REMOTE_ADDR") ."_". getenv("HTTP_USER_AGENT");
		// Or simply use a Superglobal ($_SERVER or $_ENV)
        // $ip = $_SERVER['REMOTE_ADDR'];
		$out = "";
		$online = 1;
		$_users = array();
		if (file_exists($datafile)){
			$users = explode("\n",rtrim(file_get_contents($datafile)));
			if($users){
				foreach($users as $user){
					list($usertime, $userip) = explode('|', $user);
					if ((time() - $usertime) < $seconds && $userip != $ip){
						$out .= $usertime . '|' . $userip . "\n";
						++ $online;
						$_users[]= $userip;
					}
				}
			}
		}
		$out .= time() . '|' . $ip . "\n";
		$_users[]= $ip;
		file_put_contents($datafile, $out);
		//return $online;
		return $_users;
	}


}


?>